package es.ats.codington.festival.utils;

import java.io.FileReader;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

/**
 * The Class JSONQuery.
 */
public class JSONQuery {
	
	/** The path. */
	private static String path;


	/**
	 * Instantiates a new JSON query.
	 *
	 * @param path the path
	 */
	public JSONQuery(String path){
		super();
		JSONQuery.path = path;
	}
	
	/**
	 * Gets the path.
	 *
	 * @return the path
	 */
	public static String getPath() {
		return path;
	}


	/**
	 * Sets the path.
	 *
	 * @param path the new path
	 */
	public static void setPath(String path) {
		JSONQuery.path = path;
	}


	/**
	 * Read string.
	 *
	 * @param str the str
	 * @return the string
	 */
	public String readString(String str){
        JSONParser parser = new JSONParser();
        Object obj;
        String s= new String();
        try {
        	
            obj = parser.parse(new FileReader(path));
            JSONObject jsonObject = (JSONObject) obj;
            s = (String) jsonObject.get(str);
            return s;
        } // FileNotFoundException IOException ParseException
        catch (Exception e) {
            e.printStackTrace();
            return s;
        }
    }
    
	
    /**
     * Read string from structure.
     *
     * @param structure the structure
     * @param str the str
     * @return the string
     */
    public String readStringFromStructure(String structure, String str){
        JSONParser parser = new JSONParser();
        Object obj;
        String s= new String();
        try {
            obj = parser.parse(new FileReader(path));
            JSONObject jsonObject = (JSONObject) obj;
            JSONObject struct = (JSONObject) jsonObject.get(structure);
            s = (String) struct.get(str);
            return s;
        } // FileNotFoundException IOException ParseException
        catch (Exception e) {
            e.printStackTrace();
            return s;
        }
    }
}
